package action;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


import beans.Agent;
import beans.Express;
import beans.LogPay;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.AgentDao;
import dao.BranchDao;
import dao.ExpressDao;
import dao.LogPayDao;

@SuppressWarnings("serial")
public class AgentAction extends ActionSupport implements ModelDriven<Agent> {

	private List<Agent> agents;
	private List<LogPay> logPays;
	private List<Express> expresses;

	AgentDao agentDao = new AgentDao();
	BranchDao branchDao = new BranchDao();
	ExpressDao expressDao = new ExpressDao();
	LogPayDao logPayDao = new LogPayDao();

	private int agentID;
	private Agent agent = new Agent();

	@Override
	public Agent getModel() {
		return agent;
	}

	/**
	 * 编辑代理商
	 * 
	 * @return
	 */
	public String editAgent() {
		Agent a = agentDao.getAgentById(agentID);
		agent.setId(agentID);
		agent.setIntro(a.getIntro());
		agent.setLogo(a.getLogo());
		agentDao.edit(agent);
		return "success";
	}

	/**
	 * 删除代理商(分配终端则不可删除)
	 * 
	 * @return
	 */
	public String deleteAgent() {
		if (branchDao.getBranchByAgentId(agentID).size() > 0) {
		} else {
			agent.setId(agentID);
			agentDao.delete(agent);
		}
		return "success";
	}

	/**
	 * 新增代理商
	 * 
	 * @return
	 */
	public String registerAgent() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Agent a= (Agent) session.get("admin");
		agent.setBalance(new BigDecimal(0));
		agent.setParentID(a.getId());
		agentDao.save(agent);
		return "success";
	}

	/**
	 * 通过ID获取代理商
	 */
	public String getAgentById() {
		agent = agentDao.getAgentById(agentID);
		return "success";
	}
	/**
	 * 获取代理商基础信息
	 * @return
	 */
	public String getAgentDetail() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		agent= (Agent) session.get("admin");
		return "details";
	}
	/**
	 * 获取子代理商列表
	 * @return
	 */
	public String getChildAgent() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		agent= (Agent) session.get("admin");
		agents = agentDao.getChildAgent(agent);
		return "list";
	}

	/**
	 * 获取代理商流水账
	 * 
	 * @return
	 */
	public String getAgentBill() {
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();
		Agent a = (Agent) session.get("admin");
		expresses = expressDao.getAgentExpress(a);
		logPays = logPayDao.getAllLogPay(expresses);
		return "bill";
	}

	/**
	 * 获取所有代理商
	 */
	public String getAllAgent() {
		agents = agentDao.getAllAgent();
		return "success";
	}

	public List<Agent> getAgents() {
		return agents;
	}

	public void setAgents(List<Agent> agents) {
		this.agents = agents;
	}

	public int getAgentID() {
		return agentID;
	}

	public void setAgentID(int agentID) {
		this.agentID = agentID;
	}

	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	public List<Express> getExpresses() {
		return expresses;
	}

	public void setExpresses(List<Express> expresses) {
		this.expresses = expresses;
	}

	public List<LogPay> getLogPays() {
		return logPays;
	}

	public void setLogPays(List<LogPay> logPays) {
		this.logPays = logPays;
	}

}
